﻿using System;
using System.Text;
using System.Windows.Forms;
using Tec2.GUI.Base;
using Tec2.Base;
using TecBaseObjects.Objects;
using TecData.Manager;
using TecTools;
using Tec2.Properties;
using TecExtensions;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using TecContracts;

namespace Tec2.GUI.Dialog {

    public partial class Angebot : TpBase {

        #region Members

        private string betreff = "Angebot";
        private List<Beilage> beilagen;
        private List<Person> mailCC;
        private List<Person> mailCCOutput;
        private bool setBeilagen = true;
        private const string errType = "Err";
        private const string logType = "Log";
        private const string logArea = "Tec2.GUI.Dialog.Angebot";
        private ProjectCaseObject myModel;

        #endregion

        #region Constructors

        public Angebot() {
            InitializeComponent();
        }

        public Angebot(int ProjectID, Extensions.tpWindowType tpWindow)
            : this() {
            //eventHandler.Changed += SetSelectEmpfänger;
            //eventHandler.AfterSelected += SetVersand;
            Cancel = true;
            switch(tpWindow) {
                case Extensions.tpWindowType.Mahnung:
                    this.tabAuswahl.TabPages.Remove(this.tpBeilagen);
                    this.tabAuswahl.TabPages.Remove(this.tpBetreff);
                    this.betreff = "Zahlungserinnerung";
                    this.Text = "Mahnung";
                    break;
                case Extensions.tpWindowType.Auftragsbestätigung:
                    this.betreff = "Auftragsbestätigung";
                    this.tabAuswahl.TabPages.Remove(this.tpMahnung);
                    break;
                case Extensions.tpWindowType.Korrepondenz:
                case Extensions.tpWindowType.Mail:
                    this.setBeilagen = false;
                    this.tabAuswahl.TabPages.Remove(this.tpMahnung);
                    this.betreff = "";
                    this.txtBetreff.Text = "";
                    break;
                default:
                    this.tabAuswahl.TabPages.Remove(this.tpMahnung);
                    break;
            }
            SetAngebot(ProjectID);
        }


        #endregion

        #region Properties

        public bool Cancel {
            get;
            set;
        }

        public string SelectedType {
            get { return this.selectedType; }
            set { this.selectedType = value; }
        }

        public int Versand {
            get { return this.versand; }
            set { this.versand = value; }
        }

        public ProjectCaseObject MyModel {
            get { return this.myModel; }
        }

        public Recipient MyRecipient {
            get { return this.myRecipient; }
        }

        public List<Beilage> Beilagen {
            get { return this.beilagen; }
        }

        public List<Person> MailCC {
            get { return this.mailCCOutput; }
        }

        #endregion

        #region Events

        private void Angebot_Load(object sender, EventArgs e) {
            this.txtBetreff.Text = betreff;
            this.rbMail.Checked = true;
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e) {
            SetDataBeforeClose();
            this.Close();
        }

        private void dgEmpfänger_CellClick(object sender, DataGridViewCellEventArgs e) {
            //eventHandler.ChangeSelect(sender, e);
        }

        private void dgPerson_CellClick(object sender, DataGridViewCellEventArgs e) {
            //eventHandler.ChangeSelect(sender, e);
        }

        private void gbVersand_Validated(object sender, EventArgs e) {
            //eventHandler.SelectChanged(sender, e);
            SetVersand(sender, e);
        }

        private void rbBrief_CheckedChanged(object sender, EventArgs e) {
            GlobalVar.Versand = (int)GlobalVar.VersandArt.Mail;
            //eventHandler.SelectChanged(sender, e);
        }

        private void rbFax_CheckedChanged(object sender, EventArgs e) {
            GlobalVar.Versand = (int)GlobalVar.VersandArt.Fax;
            //eventHandler.SelectChanged(sender, e);
        }

        private void rbMail_CheckedChanged(object sender, EventArgs e) {
            GlobalVar.Versand = (int)GlobalVar.VersandArt.Brief;
            //eventHandler.SelectChanged(sender, e);
        }

        #endregion

        #region Public Methods

        public void SetAkontoDatasource(List<AkontoObj> akontos) {
            this.bsAkontos.DataSource = akontos;
        }

        public void SetInvoicesDatasource(List<InvoiceObj> dsInvoice) {
            this.bsRechnungen.DataSource = dsInvoice;
        }

        public void SetPaymentDatasource(List<AkontoObj> dsPayment) {
            this.bsZahlungen.DataSource = dsPayment;
        }

        public void SetReminderDatasource(List<MahnungObj> dsReminder) {
            this.bsMahnungen.DataSource = dsReminder;
        }

        #endregion

        #region Methods

        private void btnAddBeilage(object sender, EventArgs e) {
            try {
                if(this.openFileDialog1.ShowDialog() == DialogResult.OK) {
                    string fileBezeichnung = InputBox.ShowDialog("Dateibezeichnung", "Beilage");

                    Beilage bel = new Beilage();
                    bel.FullFileName = openFileDialog1.FileName;
                    bel.IsChecked = true;
                    if(fileBezeichnung != null && fileBezeichnung.Length > 0) {
                        bel.BeilagenText = fileBezeichnung + " (" + bel.FileName + ")";
                    } else {
                        bel.BeilagenText = bel.FileName;
                    }
                    this.beilagenBindingSource.Add(bel);
                }
            } catch(Exception ex) {
                MessageBox.Show("Error" + ex.Message);
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, ex.Message, true);
            }
        }

        private void SetAngebot(string orgName, string suchbegriff) {
            //DataGridViewButtonColumn addFileName = new DataGridViewButtonColumn();
            //addFileName.Name = "Auswahl";
            //addFileName.Text = "Datei...";
            //addFileName.Width = 60;

            //this.dgBeilagen.Columns.Add(addFileName);
            //this.dgBeilagen.CellClick += new DataGridViewCellEventHandler(btnAddBeilage);

            //Organisation org = OrganisationDataManager.LoadOrganisation(orgName, suchbegriff);

            //if (org != null) {
            //    organisationBindingSource.DataSource = org;
            //    personBindingSource.DataSource = org.MyPersons;
            //    IEnumerable<Person> t1 = from t2 in org.MyPersons
            //                             where t2.EMail != null
            //                             select t2;

            //    SetMailCC();
            //    foreach (Person pers in t1) {
            //        this.mailCC.Add(pers);
            //    }
            //    mailPersonCopyBindingSource.DataSource = this.mailCC;
            //} else {
            //    string msg = String.Format(Resources.NameInDbNichtGefunden, OrgName, suchbegriff);
            //    MessageBox.Show(msg, "Dialoag Auswahl", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    Cancel = true;
            //    this.Close();
            //}

            SetBeilagen();
            beilagenBindingSource.DataSource = this.beilagen;
        }

        private void SetAngebot(int ProjectID) {
            ProjectCaseObject obj = ProjectCaseManager.LoadProjectCase(ProjectID);
            if(obj != null) {
                bsProjectContacts.DataSource = obj.MyContacts;
                this.myModel = obj;
            }
            SetBeilagen();
            beilagenBindingSource.DataSource = this.beilagen;
        }

        private void SetBeilagen() {

            if(this.setBeilagen) {
                if(Settings.Default.BeilagenFile1 != null && Settings.Default.BeilagenFile1.Length > 0) {
                    Beilage std = new Beilage();
                    std.FullFileName = Settings.Default.BeilagenFile1;
                    std.BeilagenText = Settings.Default.BeilagenFile1Text + " (" + std.FileName + ")";
                    std.IsChecked = true;
                    this.beilagen = new List<Beilage>();
                    this.beilagen.Add(std);
                }

                if(Settings.Default.BeilagenFile2 != null && Settings.Default.BeilagenFile2.Length > 0) {
                    Beilage std = new Beilage();
                    std.FullFileName = Settings.Default.BeilagenFile2;
                    std.BeilagenText = Settings.Default.BeilagenFile2Text + " (" + std.FileName + ")";
                    std.IsChecked = true;
                    if(this.beilagen != null) {
                        this.beilagen = new List<Beilage>();
                    }
                    this.beilagen.Add(std);
                }

                if(Settings.Default.BeilagenFile3 != null && Settings.Default.BeilagenFile3.Length > 0) {
                    Beilage std = new Beilage();
                    std.FullFileName = Settings.Default.BeilagenFile3;
                    std.BeilagenText = Settings.Default.BeilagenFile3Text + " (" + std.FileName + ")";
                    std.IsChecked = true;
                    if(this.beilagen != null) {
                        this.beilagen = new List<Beilage>();
                    }
                    this.beilagen.Add(std);
                }
            }

        }

        private void SetBeilagenData() {
            foreach(Beilage bel in this.beilagen) {
                if(!bel.IsChecked) {
                    this.beilagen.Remove(bel);
                    if(this.beilagen.Count == 0) {
                        break;
                    }
                }
            }
        }

        private void SetBetreff(string text, bool addText) {
            text = " " + text;
            string newText = this.txtBetreff.Text;
            if(addText) {
                newText += text;
            } else {
                newText = newText.Replace(text, "");
            }
            this.txtBetreff.Text = newText;
        }

        private void SetMailCC() {
            this.mailCC = new List<Person>();
        }

        private void SetMailCCData() {
            this.mailCCOutput = new List<Person>();

            foreach(DataGridViewRow row in this.dgMailPersonKopie.Rows) {
                Person pers = (Person)row.DataBoundItem;
                if(pers != null) {
                    if(pers.SendToThisPerson) {
                        this.mailCCOutput.Add(pers);
                    }
                }
            }

        }

        private void SetSelectEmpfänger(object sender, EventArgs e) {
            StringBuilder txt = new StringBuilder();
            DataGridView dgv = sender as DataGridView;
            DataGridViewRow row = dgv.CurrentRow;

            //if (dgv.Tag.ToString() == "Org") {
            //    SelectedType = "Org";
            //    this.chkZuHanden.Checked = false;
            //    this.chkZuHanden.Enabled = false;
            //}
            //if (dgv.Tag.ToString() == "Person") {
            //    SelectedType = "Person";
            //    this.chkZuHanden.Enabled = true;
            //}
            txt.Append(GetSelectedText());
            //this.txtAuswahl.Text = txt.ToString();
        }

        private void SetVersand(object sender, EventArgs e) {
           if (this.rbMail.Checked) {
               this.Versand = (int)GlobalVar.VersandArt.Mail;
            }
           if(this.rbFax.Checked) {
               this.Versand = (int)GlobalVar.VersandArt.Fax;
           }
           if(this.rbBrief.Checked) {
               this.Versand = (int)GlobalVar.VersandArt.Brief;
           }
        }

        private void SetRecipientData() {


            foreach(DataGridViewRow conRow in this.dgPerson.Rows) {
                ProjectContactObject con = (ProjectContactObject)conRow.DataBoundItem;
                StringBuilder anrede = new StringBuilder();
                if(con != null) {
                    if(con.Selected) {
                        this.myRecipient.Anrede = con.GetSalutation();
                        this.myRecipient.Vorname = con.Name1;
                        this.myRecipient.Nachname = con.Name2;
                        this.myRecipient.Name = con.Name1 + " " + con.Name2;
                        if(con.Sex == (byte)GlobalVar.Sex.Man) {
                            anrede.Append("Herr ");
                            anrede.Append(con.Titel1.AddSpace());
                            anrede.Append(con.Titel2.AddSpace());
                            anrede.Append(con.Name2.AddSpace());
                            anrede.Append(con.Titel3);
                            this.myRecipient.Anrede = anrede.ToString();
                        }
                        if(con.Sex == (byte)GlobalVar.Sex.Woman) {
                            anrede.Append("Frau ");
                            anrede.Append(con.Titel1.AddSpace());
                            anrede.Append(con.Titel2.AddSpace());
                            anrede.Append(con.Name2.AddSpace());
                            anrede.Append(con.Titel3);
                            this.myRecipient.Anrede = anrede.ToString();
                        }
                        if(con.Sex == (byte)GlobalVar.Sex.Organisation) {
                            anrede.Append("sehr verehrte Damen und Herren");
                            this.myRecipient.Anrede = anrede.ToString();
                        }
                        this.myRecipient.Betreff = this.txtBetreff.Text;
                        this.myRecipient.EMail = con.EMail;
                        this.myRecipient.Fax = con.Fax;
                        this.myRecipient.LKZ = con.LKZ;
                        this.myRecipient.Ort = con.Ort;
                        this.myRecipient.Plz = con.Plz;
                        this.myRecipient.PlzOrt = con.Plz + " " + con.Ort;
                        this.myRecipient.Straße = con.Straße;
                        break;
                    }
                }
            }


            SetBeilagenData();
            SetMailCCData();
        }

        private void SetDataBeforeClose() {
            SetRecipientData();
            Cancel = false;
        }

        #endregion

        #region Functions

        private string GetSelectedText() {
            string ret = "";
            StringBuilder txt = new StringBuilder();


            ret = txt.ToString();
            return ret;
        }


        #endregion

        #region Check - Events

        private void checkBox2_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox11_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox12_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox10_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox9_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }





        #endregion
        
    }
}
